Skip to content

Fix #15180 internalError in checkFormatString on invalid width or position - #8982

Open
Nussu06 wants to merge 2 commits into
cppcheck-opensource:mainfrom
Nussu06:checkio-format-width-bailout
Open

Nussu06 wants to merge 2 commits into
cppcheck-opensource:mainfrom
Nussu06:checkio-format-width-bailout

Conversation

@Nussu06

@Nussu06 Nussu06 commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

CheckIOImpl::checkFormatString converts the digits collected for a field width or a POSIX parameter position with strToInt, which throws when the digits are empty (%$d), start with a zero (%01$d, which glibc accepts) or do not fit in an int (%99999999999$d, scanf("%99999999999s", buf) with a known array), so one such format string makes CppCheck::checkFile bail out with an internalError and drop every other diagnostic for that file. Replaced the conversion with a small helper that parses the digits without throwing and clamps anything above INT_MAX, and a $ with no digits in front is no longer taken as a parameter position; widths and positions that fit in an int behave exactly as before, while an oversized one is now reported through the existing invalidScanfFormatWidth and wrongPrintfScanfParameterPositionError messages, with 2147483647 shown for the clamped value. Verified with an ASan/UBSan build: the new asserts in TestIO::testScanf4 and testPosixPrintfScanfParameterPosition throw on main and pass with the fix, the full testrunner is green, and the selfcheck options report nothing for lib/checkio.cpp. I do not have Trac access, so could you create a ticket for this? I will update the title to reference it once there is a number.

@dmcppcheck

Copy link
Copy Markdown
Contributor

There should probably be a trac ticket for this PR. Please create it, or ask us to create it. Then the PR title should be updated.

@Nussu06

Nussu06 commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

hey can anyone create trac ticket for this pr, thanks.

@danmar

danmar commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

I have created https://trac.cppcheck.net/ticket/15180

@Nussu06 Nussu06 changed the title fix internalError in checkFormatString on invalid width or position Fix #15180 internalError in checkFormatString on invalid width or position Nov 9, 2026
@Nussu06

Nussu06 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for creating the ticket. I've updated the title to reference #15180.

@danmar

danmar commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

I think there is some clang-tidy warning, the CI log says:

/home/runner/work/cppcheck/cppcheck/lib/checkio.cpp:805:17: error: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses,-warnings-as-errors]
  805 |         value = value * 10 + (c - '0');
      |                 ^~~~~~~~~~
      |                 (         )

@Nussu06

Nussu06 commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Fixed, added the parentheses around value * 10 in formatStringNumber. TestIO still passes locally.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

I found no new problems. With the PR, checkFormatString no longer throws on these format strings: %$d, %01$d, %99999999999$d, and widths too large for an int in %s, %c and %[...] scanf conversions. The positions and widths that fit in an int give the same results as before. A position or width above INT_MAX is reported as 2147483647. The PR description says so, and the new tests expect it. Two different huge positions are also clamped to the same number, so they count as one parameter. That only matters for format strings that are already wrong. No strToInt calls are left in lib/checkio.cpp. The only earlier request was the clang-tidy parentheses warning, and it is fixed.

Comments

# Comment By Where Status
1 PR should have a trac ticket and reference it in the title @dmcppcheck general ✅ Fixed
2 clang-tidy readability-math-missing-parentheses in formatStringNumber @danmar general ✅ Fixed

Testing

  • Built cppcheck and testrunner in pr/ with make -j8. It built cleanly.
  • ./testrunner TestIO: 38 tests, 0 failed (1 todo).
  • I ran pr/cppcheck -q --enable=all t.c on this file:
void f(void) {
  char s[8];
  printf("%$d %s\n", 1);
  printf("%01$d %1$d\n", 1);
  printf("%2147483648$d %2147483649$d\n", 1);
  scanf("%99999999999c", s);
  scanf("%.99999999999s", s);
  scanf("%000000000000000000007s", s);
  scanf("%0000000000000000000099999999999[a-z]", s);
  printf("%1$d %2$d %3$d\n", 1, 2, 3, 4);
  printf("%0$d %00$d\n", 1);
}

There was no internalError, and every diagnostic was what I expected:

  • %$d %s: wrongPrintfScanfArgNum.
  • %01$d %1$d: no warning, since both are position 1.
  • The positions above INT_MAX: one wrongPrintfScanfParameterPositionError, which names 2147483647.
  • The widths too large for an int in %c, %.…s and %[a-z]: invalidScanfFormatWidth, which shows 2147483647.
  • The width 7 with many leading zeros: no warning, which is correct.
  • %0$d and %00$d: "parameter positions start at 1".
  • I did not build base/ for comparison, because the PR description and the new tests already cover the old throwing behaviour.

Automated review of bb08439 by Claude. The pull request was built and tested locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants